var timeout = 3000;
var usrTime = 30000;
var timeid;

function sendChat() {
  var val = $("#edit-chattext").val();
  $("#edit-chattext").val("");
  $.ajax({
    url: '/chat/' + roomid,
    type: 'POST',
    data: {content: val},    
  });
}

function getChats() {
  $.ajax({
    url: '/chat/' + roomid + '?time=' + time,
    type: 'GET',
    success: function(data, textStatus) {
      var i = data.indexOf('|');
      rtime = data.substr(0, i);
      if (rtime > 0) {      
        chats = data.substr(i + 1);
        var chatdiv = $("#chats");
        chatdiv.append(chats);
        chatdiv.attr("scrollTop", chatdiv.attr("scrollHeight"));
        time = rtime;
        
      }
      timeid = setTimeout("getChats()", timeout);
    }
  });
}

function getUsers() {
  $.ajax({
    url: '/user/online/' + roomid,
    type: 'GET',
    success: function(data, textStatus) {
      $("#roomusers").html(data);
      setTimeout("getUsers()", usrTime);
    }
  });
}

$(document).ready(function() {
  getChats();
  setTimeout("getUsers()", 5000);
});